package com.alexgaoyh.weixin.websocket.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.core.MessageSendingOperations;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/websocket")
public class WebSocketController {

	//-----------------------服务端向客户端发送消息---------------------------------------------------------- begin
	private final MessageSendingOperations<String> messagingTemplate;

    @Autowired
    public WebSocketController(
        final MessageSendingOperations<String> messagingTemplate) {
        this.messagingTemplate = messagingTemplate;
    }
    
    @RequestMapping("/send/{content}")
    @ResponseBody
	public void send(@PathVariable("content") String content) {
    	System.out.println(content);
    	this.messagingTemplate.convertAndSend(
                "/message/weixin", content);
    }
    
    @RequestMapping("/message")
	public ModelAndView message(ModelAndView model) {
    	model.setViewName("index/message");
    	return model;
	}
    
}
